<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class Post extends Model
{
    protected $table = 'posts';

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @name 作者
     */
    public function user(){
        return $this->belongsTo(\App\Models\Users::class,'user_id','id');
    }
    /**
     * @return mixed
     * @name 返回最新数据用于首页
     * @author wudean
     */
    public static function getHomeByNewData(){
        $where=array();
        $where[]=['status','=',1];
        return DB::table('posts')
            ->where($where)
            ->orderBy('b_count','desc')
            ->orderBy('created_at','desc')
            ->paginate(15);
    }

    /**
     * @return mixed
     * @name 获取文章列表
     */
    public static function getPostListsData(){
        $where=array();
        $where[]=['status','=','1'];
        return DB::table('posts')
            ->where($where)
            ->orderBy('is_recommend','desc')
            ->orderBy('updated_at','desc')
            ->orderBy('created_at','desc')
            ->paginate(15);
    }

    /**
     * @param $id
     * @return mixed
     * @name 通过id获取一条信息
     * @author Deann.wu
     */
    public static function getInfoById($id){
         return DB::table('posts')->where('id',$id)->first();
    }

    /**
     * @param $val
     * @param string $handle_field
     * @author Deann.wu
     * @name 浏览数+1
     */
    public static function PostUpHandle($val,$handle_field="b_count"){
        return DB::table('posts')->where('id',$val)->increment($handle_field);
    }

    /**
     * @param $keyword
     * @return mixed
     * @author Deann.wu
     * @name 获取搜索数据
     */
    public static function getSearchData($keyword){
        $where=array();
        if(!empty($keyword)){
            $where[]=['title','like',"%".trim($keyword)."%"];
        }
        $where[]=['status','=','1'];
        return DB::table('posts')
            ->where($where)
            ->orderBy('is_recommend','desc')
            ->orderBy('updated_at','desc')
            ->orderBy('created_at','desc')
            ->paginate(15);

    }

    /**
     * @param $sons
     * @return mixed
     * @author Deann.wu
     * @name  根据子分类获取所数据
     */
    public static function getListAllBySon($sons){
        $where=array();
        $where[]=['status','=','1'];
        return DB::table('posts')
            ->select(self::getNeedFiled())
            ->where($where)
            ->whereIn('type_id',$sons)
            ->paginate(15);
    }

    /**
     * @param $type_id
     * @return mixed
     * @author Deann.wu
     * @name 根据分类Id
     */
    public static function getListByTypeId($type_id){
        $where=array();
        $where[]=['status','=','1'];
        $where[]=['type_id','=',$type_id];
        return DB::table('posts')
            ->select(self::getNeedFiled())
            ->where($where)
            ->paginate(15);
    }

    /**
     * @param $tid
     * @return mixed
     * @author Deann.wu
     * @name 得到分类所拥有的文章数量
     */
    public static function getCountByTypeId($tid){
        $where=array();
        $where[]=['status','=','1'];
        $where[]=['type_id','=',$tid];
        return DB::table('posts')->where($where)->count();
    }

    /**
     * @param $post
     * @return mixed
     * @author Deann.wu
     * @name 获取推荐数据
     */
    public static function getReList($post){
        $where=array();
        $where[]=['status','=','1'];
        $where[]=['type_id','=',$post->type_id];
        return DB::table('posts')
            ->where($where)
            ->orderBy('id','desc')
            ->take(4)
            ->get();
    }

    /**
     * @return array
     * @author Deann.wu
     * @name 所需要的字段
     */
    public static function getNeedFiled(){
        return [
            'id',
            'title',
            "user_id",
            'content',
            "type_id",
            "is_recommend",
            "b_count",
            "is_admin",
            "status",
            "created_at",
            "updated_at"
        ];
    }
}
